package com.example.myrecycleview;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.myrecycleview.Adapter.TonyAdapter;
import com.example.myrecycleview.Bean.TonyBean;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import com.google.android.material.snackbar.Snackbar;
import com.google.gson.Gson;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private RecyclerView recycler;
    private SwipeRefreshLayout refres;
    private FloatingActionButton floating;
    private CoordinatorLayout coordinatorLayout;
    private int clint =1;
   public List<TonyBean.ResultsBean> list = new ArrayList<>();
    private TonyAdapter tonyAdapter;

    //http://gank.io/api/data/%E7%A6%8F%E5%88%A9/16/2
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        questokhttp();
    }

    @SuppressLint("ResourceAsColor")
    private void initView() {
        //    找RecyclerView控件
        recycler = (RecyclerView) findViewById(R.id.Recycler);
        refres = (SwipeRefreshLayout) findViewById(R.id.refres);
        floating = (FloatingActionButton) findViewById(R.id.floating);
        coordinatorLayout = findViewById(R.id.coordinatorLayout);
//     设置刷新的监听
        refres.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

            }
        });
//        设置刷新的监听 旋转颜色，可以设置四层
        refres.setColorSchemeColors(R.color.design_default_color_error, R.color.colorPrimary);
//     设置刷新的背景颜色
        refres.setProgressBackgroundColorSchemeResource(R.color.colorAccent);
        floating.setOnClickListener(this);

//    设置布局管理器  设置横向排列方式
        GridLayoutManager Manager = new GridLayoutManager(this,2, LinearLayoutManager.HORIZONTAL,false);
//    设置给控件

        recycler.setLayoutManager(Manager);
    }
//    请求数据
    private  void  questokhttp(){


        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url("http://gank.io/api/data/%E7%A6%8F%E5%88%A9/16/2").build();
        Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.d("TAG", "onFailure() called with: call = [" + call + "], e = [" + e + "]");
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Log.d("", "onResponse() called with: call = [" + call + "], response = [" + response + "]");
                    final String str = response.body().toString();
                    Log.i("feqfqwef", "onResponse: dedfef"+response.headers().toString());
                    int code = response.code();
                    Headers headers = response.headers();
                    String content = response.body().string();

                    runOnUiThread(new Runnable() {

                        private TonyBean.ResultsBean resultsBean;

                        @Override
                        public void run() {
                            //                  解析数据
                            Gson gson = new Gson();
                            TonyBean tonyBean = gson.fromJson(str,TonyBean.class);
                            List<TonyBean.ResultsBean> results = tonyBean.getResults();
                            for (int i=0;i<results.size();i++){
                                resultsBean = results.get(i);
                            }
                            list.addAll((Collection<? extends TonyBean.ResultsBean>) resultsBean);
                            if (tonyAdapter==null){
                                //                       设置适配器
                                tonyAdapter = new TonyAdapter(MainActivity.this,list);
                                recycler.setAdapter(tonyAdapter);
                            }else {
//                           如果有数据就刷新
                                tonyAdapter.notifyDataSetChanged();
                            }
                        }
                    });
                }
            });
    }



    @Override
    public void onClick(View v) {
        //            悬浮按钮的点击事件
        if (v.getId() == R.id.floating) {
            Snackbar.make(coordinatorLayout, "希望有所收获", Snackbar.LENGTH_SHORT).setAction("Tony课堂", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "我希望有所收获", Toast.LENGTH_LONG).show();
                }
            }).show();
        }
    }
}
